/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DLL;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class QueryPackets {
    private String flag;
    private String data;

    public QueryPackets() {
    }
   
    public QueryPackets(String flag, String data)
    {
        this.flag = flag;
        this.data = data;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
    
    //Bao gom flag = 1,4,5,6
    //Bao gom data = ten file can down
    //Ap dung choi goi check chum,request download,stop download,wait download
    public static byte[] SendNormal(String flag, String data)
    {
        
        byte[] flags = new byte[]{1};
        flags = flag.getBytes();
        byte[] tenFile = data.getBytes();
        byte[] lengthData = new byte[4];
        Integer lData = data.length();
        lengthData[0] = (byte) ((lData >> 24) & 0xFF);
        lengthData[1] = (byte) ((lData >> 16) & 0xFF);
        lengthData[2] = (byte) ((lData >> 8) & 0xFF);
        lengthData[3] = (byte) (lData & 0xFF);
        byte[] buf = new byte[flags.length+tenFile.length+4];
        System.arraycopy(flags, 0, buf, 0, flags.length);
        System.arraycopy(lengthData, 0, buf, 1, 4 );
        System.arraycopy(tenFile, 0, buf, 5, tenFile.length);
        return buf;
    }
    
    //Bao gom flag = 2
    //Bao gom data = list data muon gui
    public static byte[] ResponNormal(String flag, List<String> data)
    {
        
        byte[] flags = new byte[]{1};
        flags = flag.getBytes();
        String dataSend = "";
        byte[] lengthData = new byte[4];
        for(int i=0;i<(data.size() - 1);i++)
        {
            dataSend = dataSend + data.get(i);
            dataSend = dataSend + ",";
        }
        dataSend = dataSend + data.get(data.size() - 1);
        Integer lData = dataSend.length();
        
        lengthData[0] = (byte) ((lData >> 24) & 0xFF);
        lengthData[1] = (byte) ((lData >> 16) & 0xFF);
        lengthData[2] = (byte) ((lData >> 8) & 0xFF);
        lengthData[3] = (byte) (lData & 0xFF);
        byte[] tenFile = dataSend.getBytes();
        byte[] buf = new byte[flags.length+tenFile.length + 4];
        System.arraycopy(flags, 0, buf, 0, flags.length);
        System.arraycopy(lengthData, 0, buf, 1, 4 );
        System.arraycopy(tenFile, 0, buf, 5, tenFile.length);
        return buf;
    }
    public static byte[] changeSequents(String flag,int sequent,String fileName)
    {
        byte[] flags = new byte[]{1};
        flags = flag.getBytes();
        // sequence number
        byte[] sequence = new byte[4];
        //int Sequen = 648;
        sequence[0] = (byte) ((sequent >> 24) & 0xFF);
        sequence[1] = (byte) ((sequent >> 16) & 0xFF);
        sequence[2] = (byte) ((sequent >> 8) & 0xFF);
        sequence[3] = (byte) (sequent & 0xFF);
        // short file name length
        short lenghFileName = (short)fileName.length();
        byte[] lengthFileNames = new byte[2];
        lengthFileNames[0] = (byte)(lenghFileName & 0xff);
        lengthFileNames[1] = (byte)((lenghFileName >> 8) & 0xff);
        byte[] datas = new byte[]{};
        datas =fileName.getBytes();
        byte[] seDatas = new byte[1024];
        System.arraycopy(flags, 0, seDatas, 0, flags.length);
        System.arraycopy(sequence, 0, seDatas, 1, sequence.length);
        System.arraycopy(lengthFileNames, 0, seDatas, 5, lengthFileNames.length);
        System.arraycopy(datas, 0, seDatas, 7, datas.length);
        return seDatas;
    }
    
    //cach doc goi tin
    //s.receive(dgp);
    //byte[] reBuf = dgp.getData();
    //Lay flag
    //char a = (char)reBuf[0];
    //Lay data
    //byte[] dataB = Arrays.copyOfRange(reBuf, 1, reBuf.length);
    //String dataReceive = new String(dataB);
    
    //Bao gom falg = 0;
    //Bao gom data = noi dung tap tin muon gui di
    
}
